package com.fmi.timedealer;

import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CategoryActivity extends Activity{
	
	public final static String CATEGORY_TO_EDIT = "CategoryToEdit";
	public final static String REULT_CATEGORY = "ResultCategory";
	
	private IDataManager dbManager = null;
	
	private Category categoryToEdit;
	private Category resultCategory;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		categoryToEdit = extras.getSerializable(CATEGORY_TO_EDIT) != null ?
				(Category) extras.getSerializable(CATEGORY_TO_EDIT) : null;
						
		final EditText categoryNameText = (EditText) findViewById(R.id.add_category_name);
		if(categoryToEdit != null) {
			categoryNameText.setText(categoryToEdit.getName());
		}
		
		Button btnCreateCategory = (Button)findViewById(R.id.btnCreateCategory);
		btnCreateCategory.setText(categoryToEdit != null ? "Edit Category" : "Create Category");
		btnCreateCategory.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				String categoryName = categoryNameText.getText() != null ? categoryNameText.getText().toString() : null;
				
				Category categoryHandled =  null;
				String categoryOldName = null;
				
				if(categoryToEdit == null) {
					categoryHandled = new Category(categoryName);
				}
				else {
					categoryOldName = categoryToEdit.getName();
					categoryToEdit.setName(categoryName);
					categoryHandled = categoryToEdit;
				}
				String messages = validateData(categoryHandled);
				
				if(messages != null && !messages.equals("")) {
					
					Toast.makeText(getApplicationContext(), "Category is not saved! \n" + messages, Toast.LENGTH_SHORT).show();
					return;
				}
				
				boolean success = false;
				String actionPerformed = "";
				
				if(categoryToEdit == null) {
					actionPerformed = "created";
					success = dbManager.insertCategory(categoryHandled);
				}
				else {
					success = dbManager.updateCategory(categoryHandled, categoryOldName);
				}
				
				if(success) {
					Toast.makeText(getApplicationContext(), "Successfully " + actionPerformed + " category '"
							+ categoryHandled.getName() + "'", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(getApplicationContext(), "A technical problem occured, category is not created", Toast.LENGTH_SHORT).show();
				}
				
				resultCategory = categoryHandled;
				returnResult(RESULT_OK);
			}
		});	
	}
	
	protected String validateData(Category category) {
		
		StringBuilder messages = new StringBuilder();
		
		if(category.getName() == null || category.getName().equals("")) {
			messages.append("'Name' is required \n");
		}
		
		return messages.toString();
	}
	
	private void returnResult(int resultCode) {

		if (resultCode == RESULT_OK) {
			getIntent().removeExtra(REULT_CATEGORY);
			getIntent().putExtra(REULT_CATEGORY, resultCategory);
		}
		this.setResult(resultCode, getIntent());
		this.finish();
	}
	
}
